package com.ssx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ssx.entity.Customer;
import com.ssx.entity.Customershare;
import com.ssx.entity.Employees;
import com.ssx.mapper.CustomerMapper;
import com.ssx.mapper.CustomershareMapper;
import com.ssx.mapper.EmployeesMapper;
import com.ssx.service.IEmployeesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ssx
 * @since 2020-10-24
 */
@Service
@Transactional
public class EmployeesServiceImpl extends ServiceImpl<EmployeesMapper, Employees> implements IEmployeesService {

    @Autowired
    private EmployeesMapper employeesMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomershareMapper customershareMapper;
    @Override
    public int resetPassord(int id) {
        return employeesMapper.resetPassord(id);
    }

    /*
     new QueryWrapper<Employees>().like("empid", search).or().like("username", search)
                            .or().like("password", search).or().like("tel", search).or().like("name", search)
                            .or().like("email", search).or().like("roleInfo",search)
     */

    /**
     * 分页条件查所有员工和经理
     * @param start
     * @param pagesize
     * @param search
     * @return
     */
    @Override
    public List<Employees> selelctPageByCondition(int start, int pagesize, String search) {
        return employeesMapper.selelctPageByCondition(new QueryWrapper<Employees>().gt("employees.roleId", 1).and(i -> i.like("empid", search).or().like("username", search)
                .or().like("password", search).or().like("tel", search).or().like("name", search)
                .or().like("email", search).or().like("roleInfo", search)).last(" limit " + start + "," + pagesize));
    }

    /**
     * 分页条件仅查员工
     * @param start
     * @param pagesize
     * @param search
     * @return
     */
    @Override
    public List<Employees> selelctPageByConditionJsutEmp(int start, int pagesize, String search) {
        return employeesMapper.selelctPageByConditionJsutEmp(new QueryWrapper<Employees>().gt("employees.roleId",2).and(i->i.like("empid", search).or().like("username", search)
                .or().like("password", search).or().like("tel", search).or().like("name", search)
                .or().like("email", search).or().like("roleInfo", search)).last(" limit " + start + "," + pagesize));
    }

    @Override
    public int selelctPageCount(String search) {
        return employeesMapper.selelctPageCount(new QueryWrapper<Employees>().gt("employees.roleId",1).and(i->i.like("employees.empid", search).or().like("username", search)
                .or().like("password", search).or().like("tel", search).or().like("name", search)
                .or().like("email", search).or().like("roleInfo", search)));
    }
    @Override
    public int selelctPageCountJsutEmp(String search) {
        return employeesMapper.selelctPageCountJsutEmp(new QueryWrapper<Employees>().gt("employees.roleId",2).and(i->i.like("empid", search).or().like("username", search)
                .or().like("password", search).or().like("tel", search).or().like("name", search)
                .or().like("email", search).or().like("roleInfo", search)));
    }

    @Override
    public int delOneEmp(int id){
        int delete=0;
        if (customerMapper.selectCusEmpid(id).size()==0){
            customershareMapper.delete(new QueryWrapper<Customershare>().eq("empid", id));
            delete = employeesMapper.deleteById(id);
        }
        return delete;
    }
}
